/*
 * @Description:
 * @Version: 1.0
 * @Autor: chen_yt
 * @Date: 2022-07-22 10:03:20
 * @LastEditors: chen_yt
 * @LastEditTime: 2022-07-22 10:16:52
 */
/**
 * @description: 返回混合后的10进制颜色String，如(234,122,255)
 *               计算公式: baseColor * percent + mixColor * (1-percent)
 * @param {String} baseColor 基础颜色，十六进制6位，如#005fe1
 * @param {String} mixColor 要混合的颜色，十六进制6位，如#ffffff
 * @param {Number} percent 基础颜色的百分比，取值0-1
 * @return {String} rgb颜色
 * @author: chen_yt
 */
export function colorMix(baseColor, mixColor = '#ffffff', percent = 0.1) {
    let baseColorIntArr = hexColor2int(baseColor)
    let mixColorIntArr = hexColor2int(mixColor)

    let arr = new Array(3)
    for (let i = 0; i < 3; i++) {
        arr[i] = baseColorIntArr[i] * percent + mixColorIntArr[i] * (1 - percent)
    }

    return `rgb(${arr.join()})`
}

/**
 * @description: 16进制转颜色10进制颜色
 * @param {*} hexColor 以#开头的6位颜色，比如#005fe1
 */
export function hexColor2int(hexColor) {
    let color = hexColor.replace('#', '')
    let arr = []
    while (arr.length * 2 < color.length) {
        let str = color.substr(arr.length * 2, 2)
        arr.push(str)
    }
    return arr.map((item) => hex2int(item))
}

/**
 * @description: 16进制转10进制
 * @param {*} hex 16进制数，不需要以0x开，大小写都行
 */
export function hex2int(hex) {
    let len = hex.length
    let arr = new Array(len)
    for (var i = 0; i < len; i++) {
        let code = hex.charCodeAt(i)
        if (48 <= code && code < 58) {
            code -= 48
        } else {
            code = (code & 0xdf) - 65 + 10
        }
        arr[i] = code
    }
    return arr.reduce(function (acc, c) {
        acc = 16 * acc + c
        return acc
    }, 0)
}